package com.adayo.memoryfree;

import android.Manifest;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.provider.Settings;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.adayo.memoryfree.activity.SettingsActivity;
import com.adayo.memoryfree.adapter.MenuAdapter;
import com.adayo.memoryfree.base.ActivityFrame;
import com.adayo.memoryfree.bean.MenuBean;
import com.adayo.memoryfree.fragment.MainFragment;
import com.adayo.memoryfree.widgets.TitleView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActivityFrame {
	@ViewInject(R.id.tv_title)
	private TitleView titleView;
	@ViewInject(R.id.lv_left)
	private ListView lvLeft;
	@ViewInject(R.id.dl_content)
	private DrawerLayout dlContent;
	private MenuAdapter menuAdapter;
	private List<MenuBean> datas = new ArrayList<>();

	@Override
	protected int getLayoutId() {
		return R.layout.activity_main;
	}

	@Override
	protected void initParams(Bundle savedInstanceState) {
		titleView.setResource(R.drawable.selector_menu,
				R.string.app_name);
		titleView.setLeftClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drawerLayoutSwitch(true);
			}
		});

		menuAdapter = new MenuAdapter(this, datas);
		lvLeft.setAdapter(menuAdapter);
		lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				listviewItemClick(i);
			}
		});
		loadData();

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		MainFragment fragment = new MainFragment();
		ft.add(R.id.container, fragment);
		ft.commitAllowingStateLoss();

		if(!hasPermission()){
			//若用户未开启权限，则引导用户开启“Apps with usage access”权限
			startActivityForResult(
					new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
					1000);
		}
	}

	private boolean hasPermission() {
		AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
		int mode = 0;
		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
			mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
					Process.myUid(), getPackageName());
		}
		return (mode == AppOpsManager.MODE_ALLOWED);
	}

	private void loadData() {
		String[] menus = getResources().getStringArray(R.array.menus);
		int[] imgs = {R.mipmap.setting_icon};
		datas.clear();
		for (int i = 0; i < menus.length; i++) {
			MenuBean bean = new MenuBean();
			Bitmap bm = BitmapFactory.decodeResource(getResources(), imgs[i]);
			bean.setIcon(bm);
			bean.setName(menus[i]);
			datas.add(bean);
		}
		menuAdapter.notifyDataSetChanged();
	}

	private void listviewItemClick(int pos) {
		switch (pos) {
			case 0:
				startActivity(SettingsActivity.class, null);
				break;
			default:
				break;
		}
	}

	@Override
	protected String[] initPermissions() {
		return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
				Manifest.permission.WRITE_EXTERNAL_STORAGE,
				Manifest.permission.READ_PHONE_STATE,
				Manifest.permission.GET_PACKAGE_SIZE,
				Manifest.permission.KILL_BACKGROUND_PROCESSES};
	}

	@Override
	public void onBackPressed() {
		if (dlContent.isDrawerOpen(Gravity.START)) {
			drawerLayoutSwitch(false);
		} else {
			super.onBackPressed();
		}
	}

	private void drawerLayoutSwitch(boolean isOpen) {
		if (isOpen) {
			dlContent.openDrawer(Gravity.START);
		} else {
			dlContent.closeDrawer(Gravity.START);
		}
	}
}
